/*
visqrt
Copyright (C) 2010  Bhupendra Aole

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
/*
 * File: VQRenderScreen.java
 * Author: Bhupendra Aole
 * Date: Sep 8, 2010: 
 * 
 * Revisions:
 * ----------
 */

package aole.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import aole.utils.SaveFileFilter;
import aole.utils.VQUtils;

/**
 * 
 */
public class VQRenderScreen extends JDialog {
	private JPanel canvas;
	private BufferedImage bi;
	private JTextField txtSave;
	private int width;

	public VQRenderScreen(JFrame parent, int width, int height) {
		super(parent, "Rendered Image");
		this.width = width;
		// hide when closing
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing (WindowEvent e) {
				super.windowClosing(e);
				e.getComponent().setVisible(false);
			}
		});

		GraphicsConfiguration gc = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getDefaultScreenDevice()
				.getDefaultConfiguration();
		// Create an image that does not support transparency
		bi = gc
				.createCompatibleImage(width, height,
						BufferedImage.TYPE_INT_RGB);

		// Render panel
		canvas = new JPanel() {
			@Override
			public void paint (Graphics g) {
				// blit the buffered image on the panel
				g.drawImage(bi, 0, 0, null);
			}
		};
		canvas.setPreferredSize(new Dimension(width, height));

		// save panel
		JPanel sp = new JPanel(new FlowLayout());
		// filename text box
		txtSave = new JTextField("untitled.jpg", 50);
		sp.add(txtSave);
		ActionListener al = new RSActionListener(this);
		// browse button
		JButton btnBrowse = new JButton("browse");
		btnBrowse.setActionCommand("file");
		btnBrowse.addActionListener(al);
		sp.add(btnBrowse);
		// save button
		JButton btnSave = new JButton("save");
		btnSave.setActionCommand("save");
		btnSave.addActionListener(al);
		sp.add(btnSave);

		// show the render frame
		add(canvas, BorderLayout.CENTER);
		add(sp, BorderLayout.SOUTH);
	}

	public void display () {
		pack();
		setVisible(true);
	}

	public String getFileName () {
		return txtSave.getText();
	}

	public void setFileName (String s) {
		txtSave.setText(s);
	}

	public RenderedImage getImage () {
		return bi;
	}

	/**
	 * @param pixel
	 * @param line
	 * @param rgb
	 */
	public void setPixel (int x, int y, int rgb) {
		bi.setRGB(x, y, rgb);
	}

	/**
	 * @param line
	 */
	public void updateImage (int line) {
		canvas.paintImmediately(0, line, width, 1);
	}
}

class RSActionListener implements ActionListener {
	private static JFileChooser fc = new JFileChooser("Save Image");
	private VQRenderScreen parent;

	public RSActionListener(VQRenderScreen p) {
		parent = p;
	}

	public void actionPerformed (ActionEvent e) {
		if (e.getActionCommand().equals("file")) {
			fc.setFileFilter(SaveFileFilter.getInstance());
			fc.setSelectedFile(new File(parent.getFileName()));
			fc.setAcceptAllFileFilterUsed(false);
			int rc = fc.showSaveDialog(parent);
			if (rc == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				parent.setFileName(f.getAbsolutePath());
			}
		} else if (e.getActionCommand().equals("save")) {
			String s = parent.getFileName();
			if (VQUtils.getExtension(s) == null) {
				parent.setFileName(parent.getFileName() + ".jpg");
				s = s + ".jpg";
			}
			File f = new File(s);
			try {
				String ext = VQUtils.getExtension(f);
				ImageIO.write(parent.getImage(), ext == null ? "jpg" : ext, f);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

}